import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 
 * @author Devin Wolanick
 * @Version 1.0
 * 
 * Control for BlackJack GUI
 */
public class BJcontrol {

	private BJmodel model;
	private BJview view;
	private OptionMenu optionMenu;
	private CountWindow countWindow;
	private int[] optionList;
	
	public BJcontrol(BJmodel model, BJview view) {
		//Set view and model
		this.view = view;
		this.model = model;
		
		OptionMenu optionMenu = new OptionMenu();
		this.optionMenu = optionMenu;
		optionMenu.addListener(new StartListener());
	}
	
	/**
	 * Resumes program after Options menu
	 */
	public void resume(int[] optionList) {
		//Set number of Player/Decks
		model.setUp(optionList);
		view.setUp(optionList[2], optionList[3]);
		
		//Add listeners
		view.addHitListeners(new HitListener());
		view.addStandListeners(new StandListener());
		view.addNextHandListener(new NextHandListener());
		view.addDebugListener(new DebugListener());
		view.addLuckyListener(new LuckyListener());
		view.addExportListener(new ExportListener());
		view.addDDListeners(new DDListener());
		view.addSurrenderListeners(new SurrenderListener());
		
		//Start Game
		model.dealTable();
		view.displayHands(model.getTable());
		view.displayValues(model.getValues());
		countWindow.takeValues(model.getWatcher().getCounts());
	}
	
	/**
	 * 
	 * @author Devin Wolanick
	 * @Version 1.0
	 * 
	 * Listener for Hit Buttons
	 */
	class HitListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	
        	int isBusted = model.getDealer().hitPlayer();
        		view.displayHands(model.getTable());
        		view.displayValues(model.getValues());
        		countWindow.takeValues(model.getWatcher().getCounts());
        	if (isBusted==1)
        		view.forceStand();
        }
    }
	
	/**
	 * 
	 * @author Devin Wolanick
	 * @Version 1.0
	 * 
	 * Listener for Stand Buttons
	 */
	class StandListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	
        	int standState = model.getDealer().standPlayer();
        	if (standState==0) {
        		view.displayHands(model.getTable());
        		view.displayValues(model.getValues());
        		countWindow.takeValues(model.getWatcher().getCounts());
        		view.enableNextPlayer();
        	}
        	else {
        		view.revealAll(model.getTable());
        		view.updateDealerValue(model.getValues());
        		countWindow.takeValues(model.getWatcher().getCounts());
        		view.enableNextPlayer();
        	}
        }
    }
	
	/**
	 * 
	 * @author Devin Wolanick
	 * @Version 1.0
	 * 
	 * Listener for Surrender Buttons
	 */
	class SurrenderListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	
        	int standState = model.getDealer().standPlayer();
        	if (standState==0) {
        		view.displayHands(model.getTable());
        		view.displayValues(model.getValues());
        		countWindow.takeValues(model.getWatcher().getCounts());
        		view.enableNextPlayer();
        	}
        	else {
        		view.revealAll(model.getTable());
        		view.updateDealerValue(model.getValues());
        		countWindow.takeValues(model.getWatcher().getCounts());
        		view.enableNextPlayer();
        	}
        }
    }
	
	/**
	 * 
	 * @author Devin Wolanick
	 * @Version 1.0
	 * 
	 * Listener for DD Buttons
	 */
	class DDListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	
        	//Hit
        	model.getDealer().hitPlayer();
    		view.displayHands(model.getTable());
    		view.displayValues(model.getValues());
    		countWindow.takeValues(model.getWatcher().getCounts());
    		
    		//Force Stand
        	int standState = model.getDealer().standPlayer();
        	if (standState==0) {
        		view.displayHands(model.getTable());
        		view.displayValues(model.getValues());
        		countWindow.takeValues(model.getWatcher().getCounts());
        		view.enableNextPlayer();
        	}
        	else {
        		view.revealAll(model.getTable());
        		view.updateDealerValue(model.getValues());
        		countWindow.takeValues(model.getWatcher().getCounts());
        		view.enableNextPlayer();
        	}
        }
    }
	
	/**
	 * 
	 * @author Devin Wolanick
	 * @Version 1.0
	 * 
	 * Listener for Next Hand Button
	 */
	class NextHandListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	model.getDealer().clearTable();
        	model.dealTable();
        	view.displayHands(model.getTable());
    		view.displayValues(model.getValues());
    		countWindow.takeValues(model.getWatcher().getCounts());
    		view.enableNextPlayer();
        }
    }
	
	/**
	 * 
	 * @author Devin Wolanick
	 * @Version 1.0
	 * 
	 * Listener Start Button in Option Menu
	 */
	class StartListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	optionMenu.closeWindow();
        	optionList = optionMenu.getOptions();
        	countWindow = new CountWindow(optionList);
        	for (int i=4; i<optionList.length; i++)
        		if (optionList[i] == 1) {
        			countWindow.setVisable(true);
        			break;
        		}
        	resume(optionList);
        }
    }
	
	/**
	 * 
	 * @author Devin Wolanick
	 * @Version 1.0
	 * 
	 * Listener for Debug Check Box
	 */
	class DebugListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	model.switchDebug();
        }
    }
	
	/**
	 * 
	 * @author Devin Wolanick
	 * @Version 1.0
	 * 
	 * Listener for Debug Check Box
	 */
	class LuckyListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	model.switchLucky();
        }
    }
	
	/**
	 * 
	 * @author Devin Wolanick
	 * @Version 1.0
	 * 
	 * Listener Export Button
	 */
	class ExportListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	model.exportData();
        }
    }
}
